package com.helloandroid.paso.ui.mediaplayer;

import java.util.List;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TrackAdapter extends ArrayAdapter<Track>{

	int viewResourceID, titleTextID;
	
	public TrackAdapter(Context context, int textViewResourceId,
			List<Track> objects, int trackTitleTextID) {
		super(context, textViewResourceId, objects);
		viewResourceID = textViewResourceId;
		titleTextID = trackTitleTextID;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent){
		LinearLayout trackView;
		
		Track track = getItem(position);
		
		if(convertView == null){
			trackView = new LinearLayout(getContext());
			LayoutInflater li = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			li.inflate(viewResourceID, trackView, true);
		}
		else{
			trackView = (LinearLayout) convertView;
		}
		
		TextView titleTextView = (TextView)trackView.findViewById(titleTextID);
		
		if(track.tag != null){
			if(!TextUtils.isEmpty(track.tag.Title)){
				titleTextView.setText(track.tag.Title);
			}
			else{
				titleTextView.setText(Helper.getFilenameFromURL(track.path));
			}
		}
		else{
			titleTextView.setText(Helper.getFilenameFromURL(track.path));
		}
		
		return trackView;
	}

}
